<?php
require_once '../dbms_university_courses_mvc/Connection.php';
require_once '../dbms_university_courses_mvc/Model/model_require_once.php';

//get available periods for this classroom 
$periods = Period::getPeriods ();
$days = Day::getDays ();

/* @var $course Course */
$course = Course::constructFrom_course_id ( $_GET ['course_id'] );

$coursePeriods = CoursePeriod::getCoursePeriods_by_course_id ( $_GET ['course_id'] );

?>

<h1>Periods for <?php
echo $course->get_course_name ();
?></h1>
<?php
/* @var $day Day */
/* @var $period Period */
//iterate the days
foreach ( $days as $day ) {
	?>
<br>
<br>
<h2><?php
	echo $day->get_day ();
	?></h2>
&nbsp;&nbsp;&nbsp;&nbsp;<?php
	foreach ( $periods as $period ) {
		
		?><span>
		<?php
		echo $period->get_period_id (); ?>
		</span>
<script type="text/javascript">
		<?php		
		$coursePeriod = periodSelected ( $period->get_period_id (), $day->get_day_id (), $coursePeriods );
		if ($coursePeriod) {
			?>
				var course_period_id<?php
			echo $period->get_period_id ();
			?><?php

			echo $day->get_day_id ();
			?> = <?php
			$coursePeriod?>;
				<?php
		} else {
			?>
		var course_period_id<?php
			echo $period->get_period_id ();
			?><?php

			echo $day->get_day_id ();
			?>;
		<?php
		}
		?>
	function periodSelected<?php
		echo $period->get_period_id ();
		?><?php

		echo $day->get_day_id ();
		?>(id){
		course_period_id<?php
		echo $period->get_period_id ();
		?><?php

		echo $day->get_day_id ();
		?> = id;
	}
</script>
<input type="checkbox" disabled="disabled"
	style="color: black;"
	<?php
		if ($coursePeriod) {
			?> checked="checked"
	<?php
		} 
		?>
/>
<?php
	}
	?>
<?php
}

function periodSelected($period_id, $day_id, $coursePeriods) {
	/* @var $coursePeriod CoursePeriod */
	foreach ( $coursePeriods as $coursePeriod ) {
		if ($coursePeriod->get_period_id () == $period_id && $coursePeriod->get_day_id () == $day_id) {
			return $coursePeriod->get_course_period_id ();
		}
	}
	return false;
}

?>
<br>
<br>
<h2>Period Reference</h2>
<table>
<?php
foreach ( $periods as $period ) {
	?><tr>
		<td>
	<?php
	echo $period->get_period_id ();
	?>
	</td>
		<td>
	<?php
	echo $period->get_start_time ();
	?>
	</td>
		<td>
	<?php
	echo $period->get_end_time ();
	?>
	</td>
	</tr>
	<?php
}
?>
</table>

